package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;

public class NaviSettingActivity extends BaseActivity {
    private final String TAG = "NaviSettingActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, NaviSettingActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navisetting_activity);

        SysUtils.getInstance().loadeUnit();

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_navis;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_navis = (ListView) findViewById(R.id.lv_navis);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<String, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_navis.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<String, String>> navisetlist;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                navisetlist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(navisetlist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    private String[] NAVISET_0 = {"逐点导航", "就近点导航"};
    private String[] NAVISET_1 = {"直线导航", "道路导航"};
    private String[] NAVISET_2 = {"驾车", "步行"};

    protected List<Entry<String, String>> getList() {

        navisetlist = new ArrayList<Entry<String, String>>();

        String strNaviSet0 = NAVISET_0[SysUtils.getInstance().navi_mode0];
        String strNaviSet1 = NAVISET_1[SysUtils.getInstance().navi_mode1];
        String strNaviSet2 = NAVISET_2[SysUtils.getInstance().navi_mode2];

        navisetlist.add(new Entry<String, String>(getString(R.string.naviset_0), strNaviSet0));
        navisetlist.add(new Entry<String, String>(getString(R.string.naviset_1), strNaviSet1));
        navisetlist.add(new Entry<String, String>(getString(R.string.naviset_2), strNaviSet2));

        return navisetlist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_navis.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onToNext(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_navis).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    private void onToNext(int pos) {
        switch (pos) {
            default:
                break;
            case 0:
                toActivity(BottomMenuWindow.createIntent(context, NAVISET_0)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.naviset_0)), REQUEST_TO_BOTTOM_MENU_0, false);
                break;
            case 1:
                toActivity(BottomMenuWindow.createIntent(context, NAVISET_1)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.naviset_1)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case 2:
                toActivity(BottomMenuWindow.createIntent(context, NAVISET_2)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.naviset_2)), REQUEST_TO_BOTTOM_MENU_2, false);
                break;
        }

    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void finish() {
        SysUtils.getInstance().saveNaviSet();
        super.finish();
    }

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    private final int REQUEST_TO_TOP_MENU = 30;

    private final int REQUEST_TO_BOTTOM_MENU_0 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_1 = 32;
    private final int REQUEST_TO_BOTTOM_MENU_2 = 33;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_0:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < 2) {
                        setListValue(0, NAVISET_0[selectedPosition]);
                        SysUtils.getInstance().navi_mode0 = selectedPosition;
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < 2) {
                        setListValue(1, NAVISET_1[selectedPosition]);
                        SysUtils.getInstance().navi_mode1 = selectedPosition;
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_2:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < 2) {
                        setListValue(2, NAVISET_2[selectedPosition]);
                        SysUtils.getInstance().navi_mode2 = selectedPosition;
                    }
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            SysUtils.getInstance().resetNaviSet();
                            initData();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }

    private void setListValue(int index, String newUnit) {
        if (navisetlist == null || navisetlist.size() != 4) {
            navisetlist = getList();
        }

        if (index < 0 || index >= navisetlist.size()) {
            return;
        }

        Entry<String, String> kvb = navisetlist.get(index);
        kvb.setValue(newUnit);

        adapter.refresh(navisetlist);
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<String, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.texttext_view, parent, false);
            }

            TextView tv_name_0 = BaseViewHolder.get(convertView, R.id.tv_name_0);
            TextView tv_name_1 = BaseViewHolder.get(convertView, R.id.tv_name_1);

            final Entry<String, String> kvb = getItem(position);

            tv_name_0.setText(kvb.getKey());
            tv_name_1.setText(kvb.getValue());

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
